﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.Data;
using FrienDev.BusinessRules;
using System.IO;

public partial class Controls_Invitation_Invitation : System.Web.UI.UserControl
{

    private User m_user;
    protected void Page_Load(object sender, EventArgs e)
    {
        m_user = UserRules.Instance.GetCurrentUser();
        txt_Name.Text = m_user.FullName;
        bt_SendMail.Click += new EventHandler(bt_SendMail_Click);
        if (!IsPostBack)
        {
            InitSendWord();
        }
    }

    void bt_SendMail_Click(object sender, EventArgs e)
    {
        string mailsubject = m_user.FullName + "邀请你加入clubof.net，并成为他的好友";
        FileStream fs = new FileStream(MapPathSecure("~/MailTemplate/FriendInvite/FriendInviteMailTemplate.txt"), FileMode.Open, FileAccess.Read);
        StreamReader m_streamReader = new StreamReader(fs);
        //使用StreamReader类来读取文件
        m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
        string str = m_streamReader.ReadToEnd();
        string mailcontent = string.Format(str, m_user.FullName, txt_InviteWords.Text, m_user.UserId);
        //关闭此StreamReader对象
        m_streamReader.Close();
        string[] email = txt_Email.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string em in email)
        {
            if (!string.IsNullOrEmpty(em.Trim()))
            {
                try
                {
                    ApiHelper.Instance.SendMail(em.Trim(), mailsubject, mailcontent, true);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('已成功邀请好友！');", true);
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('邮件发送失败！');", true);
                }
            }
        }
    }

    private void InitSendWord()
    {
        FileStream fs = new FileStream(MapPathSecure("~/MailTemplate/FriendInvite/FriendInviteWords.txt"), FileMode.Open, FileAccess.Read);
        StreamReader m_streamReader = new StreamReader(fs);
        //使用StreamReader类来读取文件
        m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
        string str = m_streamReader.ReadToEnd();
        txt_InviteWords.Text = str;
        //关闭此StreamReader对象
        m_streamReader.Close();
    }
}
